#encoding:utf8
"""
Argument Dispatcher
"""

import os
import sys

##########################################################################

class ArgumentDispatcher(object):
	def __init__(self, defautlProcessor):
		assert callable(defautlProcessor)
		self._defautlProcessor		= defautlProcessor

		self._processors			= {}
		self._processors['-h']		= self.usage
		self._processors['--help']	= self.usage

	def add(self, param, processor):
		self._processors[param]	= processor

	def run(self):
		isFind	=  False
		for arg in sys.argv[1:]:
			if arg in self._processors:
				processor	= self._processors[arg]
				print processor.__doc__
				processor()
				isFind		= True

		if not isFind:
			print self._defautlProcessor.__doc__
			self._defautlProcessor()
	
	def usage(self):
		'''show this help message and exit'''
		keys	= self._processors.keys()
		text	= 'usage: %s [%s]\n'%(os.path.basename(sys.argv[0]), '] ['.join(keys))
		arguments	= []
		for k, v in self._processors.items():
			arguments.append('	%-15s%s'%(k, v.__doc__))

		text	= '%s\noptional arguments:\n%s'%(text, '\n'.join(arguments))
		print text

##########################################################################

if __name__=='__main__':
	pass
